﻿package {
	import flash.display.*;
	import flash.geom.Matrix;
	import flash.display.Loader;
	import flash.net.URLRequest;

	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLVariables;
	import flash.net.URLRequestMethod;
	import flash.net.navigateToURL;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.system.Security;

	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.ByteArray;
	import flash.utils.Timer;
	import flash.geom.Rectangle;
	import flash.text.*;
	import gs.TweenLite;
	import ui.StateMask;



	public class main extends Sprite {
		var _loader:Loader;
		var loader:URLLoader = new URLLoader();
		var pic_box,small_box,s_pic,s_pic_box:Sprite;
		var con_mc,move_num_mc:MovieClip;
		var star_bt:SimpleButton;
		var stateMask:StateMask = new StateMask();
		var x_p:int=8;//列数
		var y_p:int=8;//行数
		var d_arr:Array=new Array();//初始数组
		var c_arr:Array=new Array();//变化数组
		var d_choose:Boolean=true;//二次点击判断
		var end_test:int;
		var click_x1,click_x2,click_y1,click_y2:int;
		var c_obj:Object;
		var num:Number;
		var step:Number=0;
		public function main() {

			begin();
		}
		//建立场景
		private function begin() {
			con_mc=new con();
			con_mc.x=510;
			con_mc.y=150;
			con_mc.row_num.type="input";
			con_mc.col_num.type="input";
			con_mc.row_num.maxChars=2;
			con_mc.col_num.maxChars=2;
			con_mc.row_num.addEventListener(Event.CHANGE,con_change);
			con_mc.col_num.addEventListener(Event.CHANGE,con_change);
			//addChild(con_mc);
			star_bt=new star_btn();
			star_bt.x=510;
			star_bt.y=340;
			star_bt.addEventListener(MouseEvent.CLICK,block_pic);
			//addChild(star_bt);
			//loadpic();
			loadurl();
		}
		//行列数改变事件
		private function con_change(e:Event) {
			x_p=con_mc.row_num.text;
			y_p=con_mc.col_num.text;
			removeChild(pic_box);
			removeChild(small_box);
			loadpic();
		}
		//读取图片link
		private function loadurl() {
			stateMask.state="数据加载中...";
			addChild(stateMask);

			var variables:URLVariables = new URLVariables();
			variables.userName="Kakera";
			variables.password="********";

			var request:URLRequest=new URLRequest("http://61.160.251.46/service/new_pintu.php");
			request.data=variables;
			request.method=URLRequestMethod.POST;

			loader.dataFormat=URLLoaderDataFormat.TEXT;
			loader.addEventListener(Event.COMPLETE, loader_complete);
			/*
			loader.addEventListener(Event.OPEN, loader_open);
			loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, loader_httpStatus);
			loader.addEventListener(ProgressEvent.PROGRESS, loader_progress);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loader_security);
			loader.addEventListener(IOErrorEvent.IO_ERROR, loader_ioError);
			*/
			loader.load(request);
		}
		//读取图片link end
		private function loader_complete(e:Event):void {
			trace(loader.data);
			loadpic(loader.data);
		}
		//读取图片
		private function loadpic(url:String="") {
			var url1:String=url.substr(0,url.indexOf("/",8));
			trace(url1);
			Security.loadPolicyFile(url1+"/crossdomain.xml");
			_loader = new Loader();
			_loader.load(new URLRequest(url));
			//_loader.load(new URLRequest("http://peach.mobinex.com:18811/wap.php"));
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoad);
		}
		private function onImageLoad(event:Event):void {
			removeChild(stateMask);
			var bitmap:BitmapData=new BitmapData(_loader.width,_loader.height);
			trace("width:"+_loader.width+"height:"+_loader.height);
			var x_num:int=480/x_p;
			var y_num:int=360/y_p;

			if ((_loader.width>480)&&(_loader.height>360)) {
				var matrix1:Matrix = new Matrix();
				matrix1.scale(480/_loader.width,360/_loader.height);
				bitmap.draw(_loader,matrix1);
			} else {
				bitmap.draw(_loader);
			}

			pic_box=new box_bg();
			pic_box.x=10;
			pic_box.y=10;
			addChild(pic_box);
			d_arr=new Array();
			for (var i:int=0; i<x_p; i++) {
				for (var j:int=0; j<y_p; j++) {
					var matrix:Matrix = new Matrix();
					matrix.translate(-x_num * i,-y_num * j);
					var block:Sprite = new Sprite();
					block.x=x_num*i;
					block.y=y_num*j;
					var temp_obj:Object={obj:block,b_x:block.x,b_y:block.y};
					d_arr.push(temp_obj);//存入初始数组
					block.buttonMode=true;
					block.graphics.lineStyle();
					block.graphics.beginBitmapFill(bitmap, matrix);
					block.graphics.drawRect(0,0,x_num-1, y_num-1);//通过背景图填充的方式分割图片
					block.graphics.endFill();
					pic_box.addChild(block);
				}
			}
			small_box=new Sprite  ;
			small_box.x=510;
			small_box.y=30;
			addChild(small_box);
			//small_box.addChild(_loader);
			small_box.width=120;
			small_box.height=90;

			block_pic();
		}
		//随机排列图片
		//private function block_pic(e:MouseEvent) {
		private function block_pic() {
			var fooba:ByteArray=new ByteArray();//复制初始数组，并随机化
			fooba.writeObject(d_arr);
			fooba.position=0;
			c_arr=fooba.readObject() as Array;
			c_arr.sort(randomsort);
			for (var i=0; i<c_arr.length; i++) {
				d_arr[i].obj.addEventListener(MouseEvent.CLICK,block_click);
				d_arr[i].obj.x=c_arr[i].b_x;
				d_arr[i].obj.y=c_arr[i].b_y;
			}
			//removeChild(con_mc);
			//removeChild(star_bt);
			move_num_mc=new move_num();
			move_num_mc.x=510;
			move_num_mc.y=150;
			addChild(move_num_mc);
		}
		//随机排列比较函数
		private function randomsort(obj_A:String,obg_B:String):int {
			return Math.floor(Math.random() * 3 - 1);
		}
		//点击事件
		private function block_click(e:MouseEvent) {
			//第一次点击
			if (d_choose) {
				click_x1=e.target.x;
				click_y1=e.target.y;
				var v_obj=e.target;
				c_obj=v_obj;
				pic_box.setChildIndex(v_obj,pic_box.numChildren-1);
				c_obj.alpha=0.7;
				d_choose=! d_choose;
			} else {
				//第二次点击
				v_obj=e.target;
				pic_box.setChildIndex(v_obj,pic_box.numChildren-1);
				c_obj.alpha=1;
				TweenLite.to(c_obj,0.3,{x:e.target.x,y:e.target.y,onComplete:check_end});
				TweenLite.to(e.target,0.3,{x:click_x1,y:click_y1,onComplete:check_end});
				d_choose=! d_choose;
				step+=1;
				move_num_mc.m_num.text=step;
			}
		}
		//比较数组，判断是否结束
		private function check_end() {
			end_test=0;
			for (var i=0; i<d_arr.length; i++) {
				(Math.floor(d_arr[i].obj.x)==d_arr[i].b_x)&&(Math.floor(d_arr[i].obj.y)==d_arr[i].b_y) ? end_test=end_test+1 : 0;
			}
			isend();
		}
		private function isend() {
			end_test==d_arr.length?win():0;
		}
		private function win() {
			small_box.addChild(_loader);
			TweenLite.to(small_box,1,{x:10,y:10,width:480,height:360});
			trace("ok");

			var variables:URLVariables = new URLVariables();
			variables.userName=loaderInfo.parameters.username;
			variables.sess=loaderInfo.parameters.sess;

			var request:URLRequest=new URLRequest("http://61.160.251.46/service/new_pintu_end.php");
			request.data=variables;
			request.method=URLRequestMethod.POST;

			loader.dataFormat=URLLoaderDataFormat.TEXT;
			//loader.addEventListener(Event.COMPLETE, loader_complete);
			/*
			loader.addEventListener(Event.OPEN, loader_open);
			loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, loader_httpStatus);
			loader.addEventListener(ProgressEvent.PROGRESS, loader_progress);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loader_security);
			loader.addEventListener(IOErrorEvent.IO_ERROR, loader_ioError);
			*/
			loader.load(request);

			var UV:URLVariables=new URLVariables  ;
			UV.user="CYPL";
			UV.info="ABCD";
			var UR:URLRequest=new URLRequest  ;
			UR.url="http://51zpic.cn";
			UR.data=UV;
			UR.method=URLRequestMethod.POST;
			navigateToURL(UR,"_blank");
		}
	}
}